package com.yxjwzpx.management.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.yxjwzpx.management.domain.FaultReport;
import com.yxjwzpx.management.mapper.FaultReportMapper;
import com.yxjwzpx.management.service.IFaultReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 故障上报Service业务层处理
 * 
 * @author yxjwzpx
 * @date 2025-04-09
 */
@Service
public class FaultReportServiceImpl implements IFaultReportService 
{
    @Override
    public int countThisMonthByUserId(Long userId) {
        return faultReportMapper.countThisMonthByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getFaultTypeDistributionByUserId(Long userId) {
        return faultReportMapper.groupByDescriptionByUserId(userId);
    }




    @Autowired
    private FaultReportMapper faultReportMapper;

    /**
     * 查询故障上报
     * 
     * @param reportId 故障上报主键
     * @return 故障上报
     */
    @Override
    public FaultReport selectFaultReportByReportId(Long reportId)
    {
        return faultReportMapper.selectFaultReportByReportId(reportId);
    }

    /**
     * 查询故障上报列表
     * 
     * @param faultReport 故障上报
     * @return 故障上报
     */
    @Override
    public List<FaultReport> selectFaultReportList(FaultReport faultReport)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        if("OWNER".equals(roles.get(0).getRoleKey())){
            if(user.getUserId()!=null){
                faultReport.setUserId(user.getUserId());
            }else{
                return new ArrayList<>();
            }
        }
        return faultReportMapper.selectFaultReportList(faultReport);
    }

    /**
     * 新增故障上报
     * 
     * @param faultReport 故障上报
     * @return 结果
     */
    @Override
    public int insertFaultReport(FaultReport faultReport)
    {
        faultReport.setCreateTime(DateUtils.getNowDate());
        return faultReportMapper.insertFaultReport(faultReport);
    }

    /**
     * 修改故障上报
     * 
     * @param faultReport 故障上报
     * @return 结果
     */
    @Override
    public int updateFaultReport(FaultReport faultReport)
    {
        faultReport.setUpdateTime(DateUtils.getNowDate());
        return faultReportMapper.updateFaultReport(faultReport);
    }

    /**
     * 批量删除故障上报
     * 
     * @param reportIds 需要删除的故障上报主键
     * @return 结果
     */
    @Override
    public int deleteFaultReportByReportIds(Long[] reportIds)
    {
        return faultReportMapper.deleteFaultReportByReportIds(reportIds);
    }

    /**
     * 删除故障上报信息
     * 
     * @param reportId 故障上报主键
     * @return 结果
     */
    @Override
    public int deleteFaultReportByReportId(Long reportId)
    {
        return faultReportMapper.deleteFaultReportByReportId(reportId);
    }
}
